<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Cities;
use App\Models\FilmTypeMaster;
use App\Models\MacroManagement;
use App\Models\MakeMaster;
use App\Models\ModelMaster;
use App\Models\PartMaster;
use App\Models\ProvinceMaster;
use App\Models\RegionMaster;
use App\Models\Warranty;
use App\Services\ModuleService;
use Encore\Admin\Grid\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class SettingMacroController extends Controller
{
    public function index(Request $request)
    {
        checkAuthority();
        $page = [
            'page_name' => 'STATICTEXTMANAGEMENT',
            'title' => ts('STATICTEXTMANAGEMENT'),
            'breadcrumb' => [
                [
                    'text' => ts('DATADICTIONARY'),
                    'url' => 'javascript:void(0)'
                ]
            ],
        ];
        $model = DB::table('macro_management');
        /*$year_id = $request->input('year_id');
        if ($year_id) {
            $model = $model->where('year_id', $year_id);
        }*/
        $model = filterByColumns($model, ['macro_name', 'english_value', 'traditional_chiness_value', 'simplified_chiness_value']);
        $menus = (new ModuleService())->getWebButton(session('admin.role_id') ?? 0, $request->path());

        $data = $model->orderByDesc('id')
            ->paginate(50);
        return view('admin.macro.index', [
            'data'=> $data,
            'page' => $page,
            'menus' => $menus,
        ]);
    }

    public function create(Request $request)
    {
        checkAuthority();
        $page = [
            'page_name' => 'STATICTEXTMANAGEMENT',
            'title' => ts('addnew'),
            'breadcrumb' => [
                [
                    'text' => ts('STATICTEXTMANAGEMENT'),
                    'url' => '/admin/setting/macro_management/'
                ]
            ],
        ];
        $data = new MacroManagement();
        return view('admin.macro.create', [
            'data'=>$data,
            'page' => $page,
        ]);
    }

    public function store(Request $request)
    {
        checkAuthority();
        $model = new MacroManagement();
        $macro_name = str_replace(' ', '', trim(strtoupper(trim($request->input('macro_name')))));
        $macro_name = str_replace(' ', '', $macro_name);
        $model->macro_name = trim($macro_name);
        $model->english_value = trim($request->input('english_value'));
        $model->traditional_chiness_value = trim($request->input('traditional_chiness_value'));
        $model->simplified_chiness_value = trim($request->input('simplified_chiness_value'));
        $model->save();
        makeLangFiles();
        return redirect('/admin/setting/macro_management')->with('trash', '新增成功！');
    }

    public function edit(Request $request, $id)
    {
        checkAuthority();
        $page = [
            'page_name' => 'STATICTEXTMANAGEMENT',
            'title' => ts('edit'),
            'breadcrumb' => [
                [
                    'text' => ts('filmtype'),
                    'url' => '/admin/setting/macro_management/'
                ]
            ],
        ];
        $data = MacroManagement::findOrFail($id);
        return view('admin.macro.edit', [
            'data'=>$data,
            'page' => $page,
        ]);
    }

    public function update(Request $request, $id)
    {
        checkAuthority();
        $model = MacroManagement::findOrFail($id);
        $macro_name = $model->macro_name;
        $macro_name = str_replace(' ', '', trim($macro_name));
        $macro_name = str_replace(' ', '', $macro_name);
        $model->macro_name = trim($macro_name);
        $model->english_value = trim($request->input('english_value'));
        $model->traditional_chiness_value = trim($request->input('traditional_chiness_value'));
        $model->simplified_chiness_value = trim($request->input('simplified_chiness_value'));
        $model->save();
        makeLangFiles();
        return redirect('/admin/setting/macro_management')->with('trash', '更新成功！');
    }

}
